//
//  MJRegisterView.m
//  MJ
//
//  Created by qiaoBu on 2017/12/11.
//  Copyright © 2017年 lina. All rights reserved.
//

#import "MJRegisterView.h"

@interface MJRegisterView ()<UITextFieldDelegate>

@property (nonatomic, strong) UIView *topView;

@end

@implementation MJRegisterView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = RGBColor(242,242,242);
        [self commonInit];
    }

    return self;
}
- (void)commonInit{
    
    [self configureTopView];
    [self configureCenterView];
}

- (void)configureTopView{
    
    _topView = [[UIView alloc] init];
    _topView.backgroundColor = RGBColor(242,242,242);
    [self addSubview:_topView];
    [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.mas_equalTo(self);
        make.height.mas_equalTo(150);
    }];
    
    _headerImageView = [[UIImageView alloc] init];
    [_topView addSubview:_headerImageView];
    [_headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.centerY.mas_equalTo(_topView);
        make.width.height.mas_equalTo(100);
    }];
    _headerImageView.userInteractionEnabled = YES;
    ViewBorderRadius(_headerImageView, 50.f, 0.5, RGBColor(217,217,217));
    
    UIButton *headerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_topView addSubview:headerBtn];
    [headerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.right.top.bottom.mas_equalTo(_headerImageView);
    }];
    headerBtn.backgroundColor = [UIColor clearColor];
    [headerBtn addTarget:self action:@selector(changeHeaderImage) forControlEvents:UIControlEventTouchUpInside];
   
    
    UIView *topLine = [[UIView alloc] init];
    [_topView addSubview:topLine];
    [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.bottom.mas_equalTo(_topView);
        make.height.mas_equalTo(0.5);
    }];
    topLine.backgroundColor = RGBColor(229,229,229);
}

- (void)configureCenterView{
    
    _nameField = [[UITextField alloc] init];
    [self addSubview:_nameField];
    [_nameField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.mas_equalTo(self);
        make.top.mas_equalTo(_topView.mas_bottom);
        make.height.mas_equalTo(55);
    }];
    _nameField.font = [UIFont systemFontOfSize:14];
    _nameField.placeholder = @"请输入昵称";
    _nameField.delegate = self;
    [_nameField setValue:RGBColor(217,217,217) forKeyPath:@"_placeholderLabel.textColor"];
    _nameField.backgroundColor = [UIColor whiteColor];
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 55)];
    leftView.backgroundColor = [UIColor whiteColor];
    _nameField.leftView = leftView;
    _nameField.leftViewMode = UITextFieldViewModeAlways;
    
    UIView *topLine = [[UIView alloc] init];
    [_topView addSubview:topLine];
    [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.mas_equalTo(_nameField);
        make.height.mas_equalTo(0.5);
        make.top.mas_equalTo(_nameField.mas_bottom);
    }];
    topLine.backgroundColor = RGBColor(229,229,229);
    
    _telephoneField = [[UITextField alloc] init];
    [self addSubview:_telephoneField];
    [_telephoneField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.mas_equalTo(self);
        make.top.mas_equalTo(topLine.mas_bottom);
        make.height.mas_equalTo(55);
    }];
    _telephoneField.delegate = self;
    _telephoneField.font = [UIFont systemFontOfSize:14];
    _telephoneField.placeholder = @"请输入手机号";
    [_telephoneField setValue:RGBColor(217,217,217) forKeyPath:@"_placeholderLabel.textColor"];
    _telephoneField.backgroundColor = [UIColor whiteColor];
    _telephoneField.returnKeyType = UIReturnKeyDone;

    UIView *telephoneLeftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 55)];
    telephoneLeftView.backgroundColor = [UIColor whiteColor];
    _telephoneField.leftView = telephoneLeftView;
    _telephoneField.leftViewMode = UITextFieldViewModeAlways;

    
    UIView *centerLine = [[UIView alloc] init];
    [self addSubview:centerLine];
    [centerLine mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.height.mas_equalTo(topLine);
        make.top.mas_equalTo(_telephoneField.mas_bottom);
    }];
    centerLine.backgroundColor = RGBColor(229,229,229);
    
    UIView *whiteView = [[UIView alloc] init];
    [self addSubview:whiteView];
    whiteView.backgroundColor = [UIColor whiteColor];
    [whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(centerLine.mas_bottom);
        make.right.mas_equalTo(self.mas_right);
        make.height.mas_equalTo(55);
        make.width.mas_equalTo(kScreenWidth);
    }];
    
    _verifyCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_verifyCodeBtn];
    
    [_verifyCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(centerLine.mas_bottom).offset(10);
        make.right.mas_equalTo(self.mas_right).offset(-10);
        make.height.mas_equalTo(34);
        make.width.mas_equalTo(130);
    }];
    [_verifyCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    _verifyCodeBtn.titleLabel.font = [UIFont systemFontOfSize:14.f];
    [_verifyCodeBtn setTitleColor:RGBColor(255,130,128) forState:UIControlStateNormal];
    _verifyCodeBtn.backgroundColor = [UIColor whiteColor];
    _verifyCodeBtn.layer.masksToBounds = YES;
    _verifyCodeBtn.layer.cornerRadius = 30/2.0f;
    _verifyCodeBtn.layer.borderWidth = 1.0f;
    _verifyCodeBtn.layer.borderColor =  RGBColor(255,130,128).CGColor;
    [_verifyCodeBtn addTarget:self action:@selector(requestVerificationCodeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    _verifyCodeField = [[UITextField alloc] init];
    [self addSubview:_verifyCodeField];
    _verifyCodeField.delegate = self;
    [_verifyCodeField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.height.mas_equalTo(_telephoneField);
        make.top.mas_equalTo(centerLine.mas_bottom);
        make.right.mas_equalTo(_verifyCodeBtn.mas_left);
    }];
    _verifyCodeField.font = [UIFont systemFontOfSize:14];
    _verifyCodeField.placeholder = @"请输入验证码";
    [_verifyCodeField setValue:RGBColor(217,217,217) forKeyPath:@"_placeholderLabel.textColor"];
    UIView *leftCodeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 55)];
    leftCodeView.backgroundColor = [UIColor whiteColor];
    _verifyCodeField.leftView = leftCodeView;
    _verifyCodeField.leftViewMode = UITextFieldViewModeAlways;
    _verifyCodeField.backgroundColor = [UIColor whiteColor];
    _verifyCodeField.returnKeyType = UIReturnKeyDone;

    UIView *bottomLine = [[UIView alloc] init];
    [self addSubview:bottomLine];
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(0.5);
        make.top.mas_equalTo(self.verifyCodeField.mas_bottom);
    }];
    bottomLine.backgroundColor = RGBColor(229,229,229);
    
    _registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_registerBtn];
    [_registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(self.mas_right).offset(-15);
        make.left.mas_equalTo(self.mas_left).offset(15);
        make.height.mas_equalTo(85/2.0);
        make.top.mas_equalTo(bottomLine.mas_bottom).offset(50);
    }];
    [_registerBtn addTarget:self action:@selector(registerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_registerBtn setTitle:@"完成" forState:UIControlStateNormal];
    [_registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _registerBtn.clipsToBounds = YES;
    _registerBtn.layer.cornerRadius = 85/4.0;
    _registerBtn.backgroundColor = RGBColor(255,130,128);
    _registerBtn.titleLabel.font = [UIFont systemFontOfSize:16.f];
    
    _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_loginBtn];
    [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(self.mas_right).offset(-5);
        make.height.mas_equalTo(30);
        make.top.mas_equalTo(bottomLine.mas_bottom).offset(10);
    }];
    [_loginBtn addTarget:self action:@selector(loginBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_loginBtn setTitleColor:RGBColor(255,130,128) forState:UIControlStateNormal];
    [_loginBtn setTitle:@"已有账号，直接登录" forState:UIControlStateNormal];
    _loginBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    
    
    NSString *str = @"注册即代表您同意爱情测试用户协议";
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:4.0f];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
    NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:@0.5, NSUnderlineStyleAttributeName, nil];
    [attributedString setAttributes:attrs range:NSMakeRange(8, 8)];
//    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(8, 8)];
    
    [attributedString addAttribute:NSUnderlineStyleAttributeName value:
     [NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(8, 8)]; // 下划线
    
    _ruleButton = [[UILabel alloc] init];
    _ruleButton.attributedText = attributedString;
    _ruleButton.font = [UIFont systemFontOfSize:12];
    _ruleButton.textColor = RGBColor(191,191,191);
    [self addSubview:_ruleButton];
    
    CGFloat h = kScreenHeight == 812 ? -45 : -20;
    [_ruleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);        
        make.bottom.mas_equalTo(self.mas_bottom).offset(h);
    }];
    _ruleButton.userInteractionEnabled = YES;
    
    }


- (void)requestVerificationCodeBtnClick:(UIButton *)button{
    
    [_telephoneField resignFirstResponder];
    [_verifyCodeField resignFirstResponder];
    [_nameField resignFirstResponder];
    
    //如果验证输入为空白,则提示:
        if (self.requestVerificationCodeBlcok){
            
            self.requestVerificationCodeBlcok([NSString stringWithFormat:@"%@",_telephoneField.text]);
        }
}
- (void)loginBtnClick:(UIButton *)button{
    
    [_telephoneField resignFirstResponder];
    [_verifyCodeField resignFirstResponder];
    [_nameField resignFirstResponder];
    
    if (self.loginBlock)
    {
        self.loginBlock();
    }
    
    [_telephoneField resignFirstResponder];
    [_verifyCodeField resignFirstResponder];
}

- (void)registerBtnClick:(UIButton *)button{
    
    [_telephoneField resignFirstResponder];
    [_verifyCodeField resignFirstResponder];
    [_nameField resignFirstResponder];
    
    //如果验证输入为空白,则提示:
     if (self.registerBlock){
            
            self.registerBlock([NSString stringWithFormat:@"%@",_telephoneField.text],[NSString stringWithFormat:@"%@",_verifyCodeField.text],[NSString stringWithFormat:@"%@",_nameField.text]);
        }
}
- (void)changeHeaderImage{
    
    if (self.changeHeaderImageBlock){
        self.changeHeaderImageBlock();
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [_nameField resignFirstResponder];
    [_telephoneField resignFirstResponder];
    [_verifyCodeField resignFirstResponder];
    return YES;
    
}

@end
